const {
  DataTypes
} = require('sequelize');
module.exports = sequelize => {
  const attributes = {
    id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: null,
      comment: "ID",
      primaryKey: true,
      field: "id",
      autoIncrement: true
    },
    parentId: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: null,
      comment: "父菜单ID",
      primaryKey: false,
      field: "parent_id",
      autoIncrement: false
    },
    treePath: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "父节点ID路径",
      primaryKey: false,
      field: "tree_path",
      autoIncrement: false
    },
    name: {
      type: DataTypes.CHAR,
      allowNull: false,
      defaultValue: null,
      comment: "菜单名称",
      primaryKey: false,
      field: "name",
      autoIncrement: false
    },
    type: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: null,
      comment: "菜单类型（1-菜单 2-目录 3-外链 4-按钮）",
      primaryKey: false,
      field: "type",
      autoIncrement: false
    },
    routeName: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "路由名称（Vue Router 中用于命名路由）",
      primaryKey: false,
      field: "route_name",
      autoIncrement: false
    },
    routePath: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "路由路径（Vue Router 中定义的 URL 路径）",
      primaryKey: false,
      field: "route_path",
      autoIncrement: false
    },
    component: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "组件路径（组件页面完整路径，相对于 src/views/，缺省后缀 .vue）",
      primaryKey: false,
      field: "component",
      autoIncrement: false
    },
    perm: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "【按钮】权限标识",
      primaryKey: false,
      field: "perm",
      autoIncrement: false
    },
    alwaysShow: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null,
      comment: "【目录】只有一个子路由是否始终显示（1-是 0-否）",
      primaryKey: false,
      field: "always_show",
      autoIncrement: false
    },
    keepAlive: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null,
      comment: "【菜单】是否开启页面缓存（1-是 0-否）",
      primaryKey: false,
      field: "keep_alive",
      autoIncrement: false
    },
    visible: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: null,
      comment: "显示状态（1-显示 0-隐藏）",
      primaryKey: false,
      field: "visible",
      autoIncrement: false
    },
    sort: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null,
      comment: "排序",
      primaryKey: false,
      field: "sort",
      autoIncrement: false
    },
    icon: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "菜单图标",
      primaryKey: false,
      field: "icon",
      autoIncrement: false
    },
    redirect: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "跳转路径",
      primaryKey: false,
      field: "redirect",
      autoIncrement: false
    },
    createTime: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: null,
      comment: "创建时间",
      primaryKey: false,
      field: "create_time",
      autoIncrement: false
    },
    updateTime: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: null,
      comment: "更新时间",
      primaryKey: false,
      field: "update_time",
      autoIncrement: false
    },
    params: {
      type: DataTypes.TEXT,
      allowNull: true,
      defaultValue: null,
      comment: "路由参数",
      primaryKey: false,
      field: "params",
      autoIncrement: false
    }
  };
  const options = {
    tableName: "sys_menu",
    comment: "",
    indexes: []
  };
  const SysMenuModel = sequelize.define("sys_menu_model", attributes, options);

  SysMenuModel.associate = function(models) {
    // 与角色菜单关联表建立关联
    SysMenuModel.hasMany(models.sys_role_menu_model, {
      foreignKey: 'menu_id',
      sourceKey: 'id'
    });
    // 与图层关联表建立关联
    SysMenuModel.hasMany(models.sys_layer_model, {
      foreignKey: 'menu_id',
      sourceKey: 'id'
    });
  };

  return SysMenuModel;
};